package org.execute.tool.excel处理.提取中的指定列;

import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVColumnExtractor {

    public static void main(String[] args) {
//        String csvFilePath = "E:\\Downloads\\trending_HK_7d_20250114-0823.csv";
//        int columnIndex = 0; // 假设你要提取第一列
//
//        List<String> columnData = extractColumnFromCSV(csvFilePath, columnIndex);
//        for (String data : columnData) {
//            System.out.println(data);
//        }

        String csvFilePath = "E:\\Downloads\\trending_HK_7d_20250114-0823.csv";
        int[] columnIndices = {0, 1, 2}; // 假设你要提取第一、第二和第三列

        Map<Integer, List<String>> columnDataMap = extractColumnsFromCSV(csvFilePath, columnIndices);
        for (Map.Entry<Integer, List<String>> entry : columnDataMap.entrySet()) {
            System.out.println("Column " + entry.getKey() + ": " + entry.getValue());
        }
    }

    public static List<String> extractColumnFromCSV(String csvFilePath, int columnIndex) {
        List<String> columnData = new ArrayList<>();
        CSVParser csvParser = new CSVParserBuilder().withSeparator(',').build();
        try (CSVReader reader = new CSVReaderBuilder(new FileReader(csvFilePath)).withCSVParser(csvParser).build()) {
            String[] nextRecord;
            while ((nextRecord = reader.readNext()) != null) {
                if (nextRecord.length > columnIndex) {
                    columnData.add(nextRecord[columnIndex]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CsvValidationException e) {
            throw new RuntimeException(e);
        }

        return columnData;
    }
    public static Map<Integer, List<String>> extractColumnsFromCSV(String csvFilePath, int[] columnIndices) {
        Map<Integer, List<String>> columnDataMap = new HashMap<>();
        for (int columnIndex : columnIndices) {
            columnDataMap.put(columnIndex, new ArrayList<>());
        }

        CSVParser csvParser = new CSVParserBuilder().withSeparator(',').build();
        try (CSVReader reader = new CSVReaderBuilder(new FileReader(csvFilePath)).withCSVParser(csvParser).build()) {
            String[] nextRecord;
            while ((nextRecord = reader.readNext()) != null) {
                for (int columnIndex : columnIndices) {
                    if (nextRecord.length > columnIndex) {
                        columnDataMap.get(columnIndex).add(nextRecord[columnIndex]);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CsvValidationException e) {
            throw new RuntimeException(e);
        }

        return columnDataMap;
    }
}

